package com.sinosoft.attachment;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.util.CheckEmpty;



@Service("attachmentManger")
public class AttachmentManagerImpl implements AttachmentManager {
	@Autowired
	AttachmentDao amdao;

	/**
	 * 获取资源列表
	 */
	public List<Attachment> getAttachments(Attachment a,int index,int pagesize){
		return amdao.getAttachments(a,index,pagesize);
	}

	/**
	 * 保存资源信息
	 */
	public Attachment saveAttachment(Attachment am){
		amdao.saveAttachment(am);
		return am;
	}
	
	public int getAttachmentNum(Attachment attachment){
		StringBuffer atcsb=new StringBuffer();
		atcsb.append("select count(*) from Attachment where 1=1 ");
		if(!CheckEmpty.isEmpty(attachment.getFileName())){
			atcsb.append(" and fileName like '%"+attachment.getFileName()+"%'");
		}
		if(null!=attachment.getIsRelease()&&attachment.getIsRelease()>0){
			atcsb.append(" and isRelease = "+attachment.getIsRelease());
		}
		if(null!=attachment.getFileType()&&attachment.getFileType()>0){
			atcsb.append(" and fileType = "+attachment.getFileType());
		}
		return amdao.getAttachment(atcsb.toString());
	}
	
	/**
	 * 根据资源id获取资源信息
	 */
	public Attachment getAttachmentById(Long achid){
		try {
			return amdao.getAttachmentById(achid);
		} catch (Exception e) {
			
			return null;
		}
	}
	
	/**
	 * 根据资源id删除该id
	 */
	public void deleteAttachmentById(Attachment ach){
		amdao.deleteAttachment(ach);
	}
}
